package fm.controller;

import fm.cache.AreaCache;
import fm.entity.Address;
import fm.exception.BizException;
import fm.web.MediaTypes;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by 61741 on 2018/2/9.
 */

@Controller
@RequestMapping("area")
public class AreaController extends BaseController {
    @RequestMapping("/getAllprovice")
    @ResponseBody
    public Map getCityOfProvince() {
        Map res = new HashMap();
        try{

            List<Address> cityList = AreaCache.getAllProvince();
            res.put("data",cityList);
            this.success(res);
        }catch (BizException ex){
            LOGGER.error("发生错误:",ex);
            this.failed(res,ex.getMessage());
        }catch (Exception ex){
            LOGGER.error("发生异常",ex);
            this.failed(res,"服务器发生了未知的错误，请联系管理员或者开发者解决!");
        }
        return res;
    }
    @RequestMapping("/getAllcity")
    @ResponseBody
    public Map getCityOfProvince(String provinceId) {
        Map res = new HashMap();
        try{

            Set<Address> cityList = AreaCache.getCityArea(provinceId);
            res.put("data",cityList);
            this.success(res);
        }catch (BizException ex){
            LOGGER.error("发生错误:",ex);
            this.failed(res,ex.getMessage());
        }catch (Exception ex){
            LOGGER.error("发生异常",ex);
            this.failed(res,"服务器发生了未知的错误，请联系管理员或者开发者解决!");
        }
        return res;
    }
       @ResponseBody
        @RequestMapping(value = "/getAllarea",method = RequestMethod.POST,produces = MediaTypes.JSON_UTF_8)
        public Map getAreaOfCity(String cityId){
            Map res = new HashMap();
            try{
                Set<Address> areaList = AreaCache.getAreaChild(cityId);
                res.put("data",areaList);
                this.success(res);
            }catch (BizException ex){
                LOGGER.error("发生错误:",ex);
                this.failed(res,ex.getMessage());
            }catch (Exception ex){
                LOGGER.error("发生异常",ex);
                this.failed(res,"服务器发生了未知的错误，请联系管理员或者开发者解决!");
            }
            return res;
        }
}
